﻿<%@ Page Language="C#" %>
<%
    string querystring = Request.Url.Query.Replace("?","");
    string[] querystringArray = querystring.Split('/');
    string levelstr = querystringArray[0];
    if (levelstr == null || levelstr == "")
        levelstr = "0";
    string cachedir = @"C:\arcgisserver\arcgiscache\test";
    double level = double.Parse(levelstr);
    string xstr = querystringArray[1];
    string ystr = querystringArray[2];
    double x = double.Parse(xstr);
    double y = double.Parse(ystr);
    string configfile = cachedir + @"\layers\conf.xml";
    if (!System.IO.File.Exists(configfile))
    {
        return;
    }
    System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
    xmlDoc.Load(configfile);
    System.Xml.XmlNode node = xmlDoc.SelectSingleNode("//TileOrigin/X");
    if (node == null)
        return;
    double xorigin = double.Parse(node.InnerText);
    node = xmlDoc.SelectSingleNode("//TileOrigin/Y");
    if (node == null)
        return;
    double yorigin = double.Parse(node.InnerText);
    node = xmlDoc.SelectSingleNode("//TileCols");
    double sizex = double.Parse(node.InnerText);
    node = xmlDoc.SelectSingleNode("//TileRows");
    double sizey = double.Parse(node.InnerText);    
    node = xmlDoc.SelectSingleNode("//LODInfo[LevelID[text()='" + level + "']]/Resolution");
    double resolution = double.Parse(node.InnerText);
    node = xmlDoc.SelectSingleNode("//LODInfo[LevelID[text()='" + (level + 1) + "']]/Resolution");
    double nextsolution = double.Parse(node.InnerText);
    double nextx1 = x * 2;
    double nexty1 = y * 2;
    double nextx2 = nextx1 + 1;
    double nexty2 = nexty1 + 1;
    double nextx3 = nextx1; 
    double nexty3 = nexty1 + 1;
    double nextx4 = nextx1 + 1;
    double nexty4 = nexty1;
     %><?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
    <Region>
      <Lod>
        <minLodPixels>256</minLodPixels><maxLodPixels>512</maxLodPixels>
      </Lod>
      <LatLonAltBox>
        <north><%=yorigin - y * resolution * sizey%></north><south><%=yorigin - (y + 1) * resolution * sizey%></south>
        <east><%=xorigin + (x + 1) * resolution * sizex%></east><west><%=xorigin + x * resolution * sizex%></west>
      </LatLonAltBox>
    </Region>
    <NetworkLink>
      <name>001120</name>
      <Region>
        <Lod>
          <minLodPixels>256</minLodPixels><maxLodPixels>512</maxLodPixels>
        </Lod>
        <LatLonAltBox>
          <north><%=yorigin - nexty4 * nextsolution * sizey%></north><south><%=yorigin - (nexty4 + 1) * nextsolution * sizey%></south>
          <east><%=xorigin + (nextx4 + 1) * nextsolution * sizex%></east><west><%=xorigin + (nextx4) * nextsolution * sizex%></west>
        </LatLonAltBox>
      </Region>
      <Link>
        <href>http://localhost/web/aspx/cache.aspx?<%=level + 1 %>/<%=nextx1 %>/<%=nexty1 %></href>
        <viewRefreshMode>onRegion</viewRefreshMode>
      </Link>
    </NetworkLink>
        <NetworkLink>
      <name>001120</name>
      <Region>
        <Lod>
          <minLodPixels>256</minLodPixels><maxLodPixels>512</maxLodPixels>
        </Lod>
        <LatLonAltBox>
          <north><%=yorigin - nexty1 * nextsolution * sizey%></north><south><%=yorigin - (nexty1 + 1) * nextsolution * sizey%></south>
          <east><%=xorigin + (nextx1+1) * nextsolution * sizex%></east><west><%=xorigin + (nextx1) * nextsolution * sizex%></west>
        </LatLonAltBox>
      </Region>
      <Link>
        <href>http://localhost/web/aspx/cache.aspx?<%=level + 1 %>/<%=nextx1 %>/<%=nexty1 %></href>
        <viewRefreshMode>onRegion</viewRefreshMode>
      </Link>
    </NetworkLink>
        <NetworkLink>
      <name>001120</name>
      <Region>
        <Lod>
          <minLodPixels>256</minLodPixels><maxLodPixels>512</maxLodPixels>
        </Lod>
        <LatLonAltBox>
          <north><%=yorigin - nexty2 * nextsolution * sizey%></north><south><%=yorigin - (nexty2 + 1) * nextsolution * sizey%></south>
          <east><%=xorigin + (nextx2+1) * nextsolution * sizex%></east><west><%=xorigin + (nextx2 ) * nextsolution * sizex%></west>
        </LatLonAltBox>
      </Region>
      <Link>
        <href>http://localhost/web/aspx/cache.aspx?<%=level + 1 %>/<%=nextx2 %>/<%=nexty2 %></href>
        <viewRefreshMode>onRegion</viewRefreshMode>
      </Link>
    </NetworkLink>
        <NetworkLink>
      <name>001120</name>
      <Region>
        <Lod>
          <minLodPixels>256</minLodPixels><maxLodPixels>512</maxLodPixels>
        </Lod>
        <LatLonAltBox>
          <north><%=yorigin - nexty3 * nextsolution * sizey%></north><south><%=yorigin - (nexty3 + 1) * nextsolution * sizey%></south>
          <east><%=xorigin + (nextx3+1) * nextsolution * sizex%></east><west><%=xorigin + (nextx3) * nextsolution * sizex%></west>
        </LatLonAltBox>
      </Region>
      <Link>
        <href>http://localhost/web/aspx/cache.aspx?<%=level + 1 %>/<%=nextx3 %>/<%=nexty3 %></href>
        <viewRefreshMode>onRegion</viewRefreshMode>
      </Link>
    </NetworkLink>
    <GroundOverlay>
      <drawOrder>5</drawOrder>
      <Icon>
        <href>http://localhost/ArcGIS/rest/services/test/MapServer/tile/<%=level %>/<%=y %>/<%=x %>.png</href>
      </Icon>
      <LatLonBox>
        <north><%=yorigin - y * resolution * sizey%></north><south><%=yorigin - (y + 1) * resolution * sizey%></south>
        <east><%=xorigin + (x+1)*resolution * sizex %></east><west><%=xorigin + (x) * resolution * sizex%></west>
      </LatLonBox>
    </GroundOverlay>
    </Document>
</kml>